11152. The last symbol
Print the last character of the string.
Input. One string with
a length not exceeding 100 characters.
Output. Print the last character of the string.
Sample
input |
Sample
output |
This is my home |
e |
string
Algorithm analysis
Let s be the input string. Its last character can be
accessed using the back()
method.
The problem
can also be solved by calculating the index of the last character of the
string. It is i = s.size()
– 1, and the character itself
can be accessed as s[i].
Algorithm implementation
Read the
input string. Print the last character.
getline(cin, s);
cout << s.back() << endl;
Algorithm implementation – index of the last character
Read the
input string.
getline(cin, s);
Compute the
position i of the last character.
i = s.size() - 1;
Print the last character.
cout << s[i] << endl;
Python implementation
Read the
input string.
s = input()
Print the last character. Use negative indexing.
print(s[-1])